package com.example.mall.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mall.constants.BaseConstants;
import com.example.mall.domain.LogisticsCompanies;
import com.example.mall.domain.LogisticsOrders;
import com.example.mall.dto.LogisticsCompaniesDto;
import com.example.mall.dto.LogisticsOrdersDto;
import com.example.mall.mapper.LogisticsOrdersMapper;
import com.example.mall.query.LogisticsCompaniesQuery;
import com.example.mall.query.LogisticsOrdersQuery;
import com.example.mall.query.PageDTO;
import com.example.mall.service.ILogisticsOrdersService;

import com.example.mall.util.AssertUtil;
import com.example.mall.util.CodeGenerateUtils;
import com.example.mall.util.CopyUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

/**
 * <p>
 * 物流单表 服务实现类
 * </p>
 *
 * @author whale
 * @since 2025-05-16
 */
@Service
public class LogisticsOrdersServiceImpl extends ServiceImpl<LogisticsOrdersMapper, LogisticsOrders> implements ILogisticsOrdersService {

    @Autowired
    private LogisticsOrdersMapper logisticsOrdersMapper;

    @Override
    public PageDTO<LogisticsOrdersDto> queryPage(LogisticsOrdersQuery logisticsOrdersQuery) {
        if (logisticsOrdersQuery == null) {
            logisticsOrdersQuery = new LogisticsOrdersQuery();
        }
        Page<LogisticsOrders> page = logisticsOrdersQuery.toMpPageByDefaultCreateTimeAsc();
        Page<LogisticsOrders> cartPage = lambdaQuery()
                .like(logisticsOrdersQuery.getLogisticsId()!= null, LogisticsOrders::getLogisticsId, logisticsOrdersQuery.getLogisticsId())
                .like(logisticsOrdersQuery.getOrderId()!= null, LogisticsOrders::getOrderId, logisticsOrdersQuery.getOrderId())
                .like(logisticsOrdersQuery.getCompanyId()!= null, LogisticsOrders::getCompanyId, logisticsOrdersQuery.getCompanyId())
                .like(logisticsOrdersQuery.getTrackingNumber()!= null, LogisticsOrders::getTrackingNumber, logisticsOrdersQuery.getTrackingNumber())
                .like(logisticsOrdersQuery.getStatus()!= null, LogisticsOrders::getStatus, logisticsOrdersQuery.getStatus())
                .orderByDesc(LogisticsOrders::getUpdateDate)
                .page(page);
        return PageDTO.of(cartPage, LogisticsOrdersDto.class);
    }

    @Override
    public void insert(LogisticsOrdersDto logisticsOrdersDto) {
        LogisticsOrders logisticsOrders = new LogisticsOrders();
        CopyUtils.copyProperties(logisticsOrdersDto, logisticsOrders);
        logisticsOrders.setLogisticsId(CodeGenerateUtils.generateUniqueId());
        logisticsOrders.setCreateDate(new Date());
        logisticsOrders.setUpdateDate(new Date());
        logisticsOrders.setStatus(BaseConstants.EFFECTIVE_STATUS);
        logisticsOrdersMapper.insert(logisticsOrders);
    }

    @Override
    public void update(LogisticsOrdersDto logisticsOrdersDto) {
        AssertUtil.isNotNull(logisticsOrdersDto.getLogisticsId(), "物流单ID不能为空");
        LogisticsOrders logisticsOrders = logisticsOrdersMapper.selectById(logisticsOrdersDto.getLogisticsId());
        AssertUtil.isNotNull(logisticsOrders, "物流单不存在");
        CopyUtils.copyProperties(logisticsOrdersDto, logisticsOrders);
        logisticsOrders.setUpdateDate(new Date());
        logisticsOrdersMapper.updateById(logisticsOrders);
    }

    @Override
    public void delete(Long logisticsId) {
        AssertUtil.isNotNull(logisticsId, "物流单ID不能为空");
        LogisticsOrders logisticsOrders = logisticsOrdersMapper.selectById(logisticsId);
        AssertUtil.isNotNull(logisticsOrders, "物流单不存在");
        logisticsOrders.setStatus(BaseConstants.LOSE_STATUS);
        logisticsOrders.setUpdateDate(new Date());
        logisticsOrdersMapper.updateById(logisticsOrders);
    }

    @Override
    public void deleteBatch(List<Long> logisticsIds) {
        AssertUtil.isNotNull(logisticsIds, "物流单ID列表不能为空");
        List<LogisticsOrders> logisticsOrders = logisticsOrdersMapper.selectBatchIds(logisticsIds);
        AssertUtil.isNotNull(logisticsOrders, "物流单不存在");
        for (LogisticsOrders logisticsOrder : logisticsOrders) {
            logisticsOrder.setStatus(BaseConstants.LOSE_STATUS);
            logisticsOrder.setUpdateDate(new Date());
            logisticsOrdersMapper.updateById(logisticsOrder);
        }
    }

    @Override
    public LogisticsOrders queryLogisticsOrdersById(Long logisticsId) {
        AssertUtil.isNotNull(logisticsId, "物流单ID不能为空");
        return logisticsOrdersMapper.selectById(logisticsId);
    }

    @Override
    public List<LogisticsOrders> queryLogisticsOrdersList() {
        return this.list();
    }
}
